#!/usr/bin/perl -w

#use Strict;
#use English;
use Switch;

#variables
$mode = 0660;

# general hashes
$words_in_email_file = "words_in_email_file";
$words_in_spam_file = "words_in_spam_file";
$phrases_in_email_file = "phrase_in_email_file";
$phrases_in_spam_file = "phrase_in_spam_file";
$whitelist_file = "whitelist_file";

# named entry hashes
$globals_file = "globals";

# named entries in "%globals"
$total_emails = "total_emails";
$total_corpus = "total_corpus";
$prob_spam = "probability_spam";
$spam_thresh = "spam_threshold";

dbmopen(%words_email, $words_in_email_file, $mode);
dbmopen(%words_spam, $words_in_spam_file, $mode);
dbmopen(%phrases_in_email, $phrases_in_email_file, $mode);
dbmopen(%phrases_in_spam, $phrases_in_spam_file, $mode);
dbmopen(%whitelist, $whitelist_file, $mode);
dbmopen(%globals, $globals_file, $mode);

#foreach $arg (@ARGV) {
#	print "Arg: ${arg}\n";
#}

if ($ARGV[0] eq "setup") {
	$globals{$prob_spam} = 0;
	$globals{$spam_thresh} = $ARGV[1];
} elsif ($ARGV[0] eq "prime_spam") {
	$words_spam{$ARGV[1]}++;
} elsif ($ARGV[0] eq "prime_whitelist") {
	# add the word(s) to the whitelist
	open(WHITELIST, '<', $ARGV[1]);
	chomp(@lines = <WHITELIST>);
	close(WHITELIST);
	foreach $line (@lines) {
		# print "Parsing Line: ${line}\n";
		@words = split(m/\s+/, $line);
		foreach $word (@words) {
			$whitelist{$word}++;
		}
	}
} elsif ($ARGV[0] eq "spam") {
	# take all the words in the spam and put them into the words_spam, and the words_email
	open(EMAIL, '<', $ARGV[1]);
	chomp(@lines = <EMAIL>);
	close(EMAIL);
	
	print "Processing file ${ARGV[1]}\n";
	foreach $line (@lines) {
		# take the input file and break it into phrases and then words
		@words = split(m/\s+/,$line);
		foreach $word (@words) {
			if (!defined $whitelist{$word}) {
				$words_spam{$word}++;			
				$words_email{$word}++;
			}
		}
	}

	$key_count = keys(%words_spam);
	print "$key_count words in the spam hash\n";

	# increase the total corpus count
	$globals{$total_corpus}++;

	# increase the total number of emails
	$globals{$total_emails}++;

} elsif ($ARGV[0] eq "determine") {
	# take the name of the input file and assign it to a read file
	open(EMAIL, '<', $ARGV[1]);
	chomp(@lines = <EMAIL>);
	close(EMAIL);

	foreach $line (@lines) {
		# take the input file and break it into phrases and then words
		@words = split(m/\w+/,$line);
		$prob_sw = 0;
		foreach $word (@words) {
			# determine what is the probability of finding this word in spam
			$prob_ws = $words_spam{$word}/$globals{$total_corpus};
			$prob_we = $words_email{$word}/$globals{$total_emails};
			$prob_sw += ($prob_ws * $globals{$prob_spam})/$prob_we;
		}
		print "Unadjusted score: ${prob_sw}\n";
		$prob_sw /= $globals{$total_emails};
		print "Adjusted Score: ${prob_sw}\n";
	}	
	 
	if ($prob_sw > $globals{$spam_thresh})
	{
		# it's probably spam.
		print "This email was probably spam!\n";
		
		# increase the total corpus count
		$globals{$total_corpus}++;
	
		# add the words to the spam corpus
		foreach $word (@words) { $words_spam{$word}++; }
	}	
		
	# UPDATE THE DATABASES	
		
	# increase the total number of emails
	$globals{$total_emails}++;

	# put those words into the words_email hash
	foreach $word (@words) { $words_email{$word}++;	}

} elsif ($ARGV[0] eq "debug") {
	print "Email Words\n" unless keys(%words_email) == 0;
	foreach $word (sort(keys(%words_email))) {
		print "Word: ${word} Count: ${words_email{${word}}}\n";
	}
	print "Spam Words\n" unless keys(%words_spam) == 0;
	foreach $word (sort(keys(%words_spam))) {
		print "Word: ${word} Count: ${words_spam{${word}}}\n";
	}
	#print "Whitelist Words\n" unless keys(%whitelist) == 0;
	#foreach $word (sort(keys(%whitelist))) {
	#	print "Word: ${word} Count: ${whitelist{${word}}}\n";
	#}
} else {

}

# close everything
dbmclose(%words_email);
dbmclose(%words_spam);
dbmclose(%phrases_in_email);
dbmclose(%phrases_in_spam);
dbmclose(%whitelist);
dbmclose(%globals);
